package com.rigsofrods.truckeditor.rig.parser;

import java.text.ParseException;
import java.util.InputMismatchException;
import java.util.Locale;
import java.util.Scanner;

import com.rigsofrods.truckeditor.rig.Node;
import com.rigsofrods.truckeditor.rig.TruckModel;


public class NodeParser implements IRigParser {
	@Override
	public boolean parseLine(String line, TruckModel truck) throws ParseException {
		int id;
		double x,y,z;
		String opt = "";
		
		Scanner scnNode = new Scanner(line);
		scnNode.useLocale(Locale.US);
		scnNode.useDelimiter("(\\s+|,\\s*)");
		try{
			id = scnNode.nextInt();
			x = scnNode.nextDouble();
			y = scnNode.nextDouble();
			z = scnNode.nextDouble();
			if(scnNode.hasNext()) opt = scnNode.next();
		}catch (InputMismatchException e) {
			return false;
		}finally{
			scnNode.close();
		}
		/*This check does not apply when I parse lines independent on the file (order may change)
		 * if(id != truck.getNodeCount()) throw new ParseException("Non-consecutive node number: " + id, 0);*/
		
		Node node = new Node(id, x, y, z, opt);
		truck.addNode(node);
		return true;
	}

	@Override
	public String getErrorMessage() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean requestComments() {
		// TODO Read groups
		return false;
	}
}
